<template>
  <div>
    <el-card class="box-card">
      <PageTools>
        <template v-slot:after>
          <el-button
            v-if="checkPerm('addroles')"
            type="primary"
            @click="addFn"
          >添加角色</el-button>
        </template>
      </PageTools>
      <el-table :data="tableData" style="width: 100%" border>
        <el-table-column prop="title" label="角色名称" />
        <el-table-column prop="description" label="角色描述" />
        <el-table-column prop="address" label="操作">
          <template v-slot="{ row }">
            <el-button
              v-if="checkPerm('editpermissionsroles')"
              type="primary"
              @click="distpermission(row.id)"
            >分配权限</el-button>
            <el-button
              v-if="checkPerm('editroles')"
              @click="editFn(row.id)"
            >编辑</el-button>
            <el-button
              v-if="checkPerm('delroles')"
              type="danger"
              @click="delFn(row.id)"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <!-- 新增弹窗 -->
    <el-dialog
      :title="formData.id ? '编辑角色' : '新增角色'"
      :visible="showDialog"
      @close="closeFn"
    >
      <el-form ref="form" label-width="120px" :model="formData" :rules="rules">
        <el-form-item label="角色名称" prop="title">
          <el-input
            v-model="formData.title"
            placeholder="请输入角色名称"
            style="width: 80%"
          />
        </el-form-item>
        <el-form-item label="角色描述" prop="description">
          <el-input
            v-model="formData.description"
            placeholder="请输入角色描述"
            style="width: 80%"
          />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="closeFn">取 消</el-button>
        <el-button type="primary" @click="comfirm">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 分配权限 -->
    <el-dialog
      title="提示"
      :visible.sync="dialogVisible"
      width="30%"
      @close="closeTree"
    >
      <el-tree
        ref="tree"
        node-key="id"
        :data="premissionsList"
        :props="{ label: 'title' }"
        check-strictly
        show-checkbox
        default-expand-all
        expand-on-click-node
      />

      <span slot="footer" class="dialog-footer">
        <el-button @click="closeTree">取 消</el-button>
        <el-button type="primary" @click="editPermissionsFn">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {
  getRolesList,
  addRole,
  getRole,
  editRole,
  deleteRole
} from '@/api/roles'
import { getPermissionsList } from '@/api/permissions'

export default {
  data() {
    return {
      dialogVisible: false,
      showDialog: false,
      tableData: [],
      formData: {
        title: '',
        description: '',
        id: '',
        permissions: []
      },
      premissionsList: [],
      permIds: [],
      roleId: '',
      rules: {
        title: [
          { required: true, message: '角色名称不能为空!', trigger: 'blur' }
        ],
        description: [
          { required: true, message: '角色描述不能为空!', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.getRolesList()
    this.getPermissionsList()
    // console.log(this)
  },
  methods: {
    // 获取角色列表
    async getRolesList() {
      const res = await getRolesList()
      // console.log(11, res)
      this.tableData = res.data
    },
    // 确认按钮---确认编辑新增
    async comfirm() {
      // 判断编辑还是新增
      if (this.formData.id) {
        await editRole(this.formData)
      } else {
        // 调用新增角色接口
        await addRole(this.formData)
      }
      // 重新获取列表
      this.getRolesList()
      // 关闭弹窗
      this.closeFn()
    },
    // 新增角色按钮
    addFn() {
      this.showDialog = true
    },
    // 编辑角色
    async editFn(id) {
      // console.log(id)
      // 打开弹窗
      this.showDialog = true
      // 信息回显
      const res = await getRole(id)
      // console.log(res)
      this.formData = res.data
    },
    // 删除角色
    delFn(id) {
      this.$confirm('是否确认删除', '删除', {
        type: 'warning'
      })
        .then(async() => {
          // 调用删除接口
          await deleteRole(id)
          // 重新渲染客户列表
          this.getRolesList()
        })
        .catch(() => {
          // console.log('取消删除')
          this.$message('取消删除')
        })
    },
    // 关闭方法
    closeFn() {
      // 关闭弹窗
      this.showDialog = false
      //  清空内容
      this.formData = {
        title: '',
        description: '',
        id: '',
        permissions: []
      }
      // 重置校验
      this.$refs.form.resetFields()
    },
    // 封装获取权限列表
    async getPermissionsList() {
      const res = await getPermissionsList()
      this.premissionsList = this.PermissionscountTree(res.data, '0')
    },
    // 获取拥有的权限函数
    async getRole(id) {
      const res = await getRole(id)
      this.formData.id = id
      // 存储权限数据
      this.permIds = res.data.permissions
      this.$refs.tree.setCheckedNodes(this.permIds)
    },
    // 分配权限按钮
    async distpermission(id) {
      // console.log(id)
      this.dialogVisible = true
      this.getRole(id)
    },
    // 转树形结构的递归函数
    PermissionscountTree(data, id) {
      const arr = []
      data.forEach((item) => {
        if (item.pid === id + '') {
          item.children = this.PermissionscountTree(data, item.id)
          arr.push(item)
        }
      })
      return arr
    },
    closeTree() {
      // 关闭树形弹窗
      this.dialogVisible = false
    },
    // 权限分配确认按钮
    async editPermissionsFn() {
      console.log(this.formData.id, this.$refs.tree.getCheckedKeys())
      await editRole({
        id: this.formData.id,
        permissions: this.$refs.tree.getCheckedKeys()
      })
      this.dialogVisible = false
      this.formData = {
        title: '',
        description: '',
        id: '',
        permissions: []
      }
    }
  }
}
</script>
 <style lang="scss" scoped>
.box-card {
  margin: 15px;
}
</style>
